/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dreamwar.view;

import dreamwar.controller.GUIController;
import dreamwar.model.MyEntityManagerFactory;
import dreamwar.model.POJO.Feature;
import dreamwar.model.POJO.Weapon;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Berto_Romero
 */
public class WeaponListPanel extends javax.swing.JPanel {

    /**
     * Creates new form WeaponListJPanel
     */
    public WeaponListPanel() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = java.beans.Beans.isDesignTime() ? null : MyEntityManagerFactory.getInstance().getEntityManagerFactory().createEntityManager();
        weaponQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT w FROM Weapon w");
        weaponList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : weaponQuery.getResultList();
        weaponScrollPane = new javax.swing.JScrollPane();
        weaponTable = new javax.swing.JTable();
        keypadPanel = new javax.swing.JPanel();
        createButton = new javax.swing.JButton();
        modifyButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();

        java.awt.GridBagLayout layout = new java.awt.GridBagLayout();
        layout.columnWidths = new int[] {0};
        layout.rowHeights = new int[] {0, 5, 0};
        setLayout(layout);

        weaponTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, weaponList, weaponTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${weaponName}"));
        columnBinding.setColumnName("Weapon Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${strength}"));
        columnBinding.setColumnName("Strength");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${featureCollection}"));
        columnBinding.setColumnName("Feature Collection");
        columnBinding.setColumnClass(java.util.Collection.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        weaponScrollPane.setViewportView(weaponTable);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.1;
        gridBagConstraints.weighty = 0.1;
        gridBagConstraints.insets = new java.awt.Insets(25, 25, 0, 25);
        add(weaponScrollPane, gridBagConstraints);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 5, 0, 5, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        keypadPanel.setLayout(jPanel1Layout);

        createButton.setText("Crear");
        createButton.setMaximumSize(new java.awt.Dimension(75, 23));
        createButton.setMinimumSize(new java.awt.Dimension(75, 23));
        createButton.setPreferredSize(new java.awt.Dimension(75, 23));
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(createButton, gridBagConstraints);

        modifyButton.setText("Modificar");
        modifyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modifyButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(modifyButton, gridBagConstraints);

        deleteButton.setText("Borrar");
        deleteButton.setMaximumSize(new java.awt.Dimension(75, 23));
        deleteButton.setMinimumSize(new java.awt.Dimension(75, 23));
        deleteButton.setPreferredSize(new java.awt.Dimension(75, 23));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        keypadPanel.add(deleteButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(0, 45, 25, 45);
        add(keypadPanel, gridBagConstraints);

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void modifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifyButtonActionPerformed
//        int[] selectedIndices = jList2.getSelectedIndices();
//        if (selectedIndices.length == 0) return;
//        List<Weapon> selectedArmyList = new ArrayList<>();
//        for (int selectedIndex : selectedIndices) {
//            selectedArmyList.add(weaponList.get(selectedIndex));
//        }
////        GUIController.getInstance().prepareModifyArmyJPanel(selectedArmyList);
//        GUIController.getInstance().changeMainFrameCard("card2");
    }//GEN-LAST:event_modifyButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
//        int[] selectedIndices = jList2.getSelectedIndices();
//        if (selectedIndices.length == 0) return;
//        List<Integer> idArmyList = new ArrayList<>();
//        for (int selectedIndex : selectedIndices) {
////            idArmyList.add(weaponList.get(selectedIndex).getIdArmy());
//        }
//        for (int idArmy : idArmyList) {
////            armyController.deleteArmy(this, idArmy);
//        }
//        jList2.setSelectedIndex(0);
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        GUIController.getInstance().changeMainFrameCard("card3");
    }//GEN-LAST:event_createButtonActionPerformed

    private String getRange() {
        return "";
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createButton;
    private javax.swing.JButton deleteButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JPanel keypadPanel;
    private javax.swing.JButton modifyButton;
    private java.util.List<dreamwar.model.POJO.Weapon> weaponList;
    private javax.persistence.Query weaponQuery;
    private javax.swing.JScrollPane weaponScrollPane;
    private javax.swing.JTable weaponTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private class featureTableModel extends AbstractTableModel {
        
        String[] columnHeader = {"Nombre", "Fuerza", "Rango", "Rasgos"};

        @Override
        public int getRowCount() {
            return weaponList.size();
        }

        @Override
        public int getColumnCount() {
            return columnHeader.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Weapon weapon = weaponList.get(rowIndex);
                switch (columnIndex) {
                    case 0:
                        return weapon.getIdWeapon();
                    case 1:
                        return weapon.getWeaponName();
                    case 2:
                        if (weapon.getCloseRange() == null) {
                            if (weapon.getLongRange() == null)
                                return "";
                            return weapon.getLongRange();
                        }
                        return weapon.getCloseRange() + " / " + weapon.getLongRange();
                    case 3:
                        if (weapon.getFeatureCollection().isEmpty())
                            return "";
                        String featureNames = "";
                        for (Feature feature : weapon.getFeatureCollection()) {
                            featureNames += feature.getFeatureName() + ", ";
                        }
                        return featureNames.substring(0, featureNames.length() - 2);
                }
                return null;
        }
        
    }

}
